//
//  ZDDHomeTableViewController.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/6/25.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit
import AFNetworking

private let statusCell = "statusCell"

class ZDDHomeTableViewController: ZDDVisitorTableViewController {
    // 定义保存微博数据的数组
    var statusArray:[ZDDStatuses] = [ZDDStatuses]()
    
    let refresh = ZDDRefresh()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //判断是否登录
        
        if !islogin{
        
            visitorView?.setupVisitorViewInfor(nil, title: nil)
        
            return
        }
        setTableViewInfo()
        loadStatusData(activityView.isAnimating())
    }

    func setTableViewInfo(){
    
    tableView.registerClass(ZDDStatusCell.self,forCellReuseIdentifier: statusCell)

//    tableView.rowHeight = 300
        //自动计算行高
    tableView.rowHeight = UITableViewAutomaticDimension
      //预估行高
    tableView.estimatedRowHeight = 200
//设置分割线
    tableView.separatorStyle = .None
    tableView.tableFooterView = activityView
//    tableView.tableHeaderView = activityView
        // 添加事件
    refresh.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)
        
        tableView.addSubview(refresh)
  
    }
   @objc private func refreshAction(){
    
        loadStatusData(activityView.isAnimating())
    
    
    }
    private lazy var activityView : UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = ThemeColor
        return view
    }()
    
}
extension ZDDHomeTableViewController {

    func loadStatusData(pullUp:Bool){
 
        var sinceId : Int64 = 0
        var maxId : Int64 = 0
        //上啦更多
        
        if pullUp {
        
            maxId = statusArray.last?.id ?? 0
            if maxId > 0 {
            
                maxId -= 1
            }
         
        }else{
        
            //下拉刷新
            sinceId = statusArray.first?.id ?? 0
            if sinceId > 0 {
            
                sinceId += 1
                
            }
        }
        
        
        ZDDNetworkTools.sharedTools.homeloadStatusData(sinceId, maxId: maxId, success: { (response) -> () in
            // 菊花停止转动
            self.endRefreshing()
            guard let res = response?["statuses"] as? [[String: AnyObject]]else{
                
                print("返回数据为nil 或者类型不匹配")
                return
            }
            var tempArray:[ZDDStatuses] = [ZDDStatuses]()
            
            for dic in res{
                
                let status = ZDDStatuses(dict: dic)
                
                tempArray.append(status)
                
            }
            if pullUp {
            
                self.statusArray = self.statusArray + tempArray
            }else{
            
                self.statusArray = tempArray + self.statusArray
            }
            
            
            //刷新UI
            self.tableView.reloadData()
//            self.activityView.stopAnimating()
            
            }) { (error) -> () in
                 print("失败:\(error)")
        }
    
     
    }
    func endRefreshing(){
        self.activityView.stopAnimating()
        self.refresh.endRefreshing()
    }
    }
extension ZDDHomeTableViewController{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(statusCell, forIndexPath: indexPath) as! ZDDStatusCell
  
        cell.status = statusArray[indexPath.row]
        
        return cell
    }
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if statusArray.count == indexPath.row + 1 && !activityView.isAnimating(){
        
             activityView.startAnimating()
            //请求数据
            loadStatusData(activityView.isAnimating())
             print(indexPath.row)
            
        }
        
        
    }



}





















